package com.ruoyi.web.controller.pack;

import java.util.List;
import java.util.Map;
import java.util.HashMap;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.pack.domain.PackTransferRecord;
import com.ruoyi.pack.service.IPackTransferRecordService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 移库记录Controller
 * 
 * @author ruoyi
 * @date 2024-12-27
 */
@RestController
@RequestMapping("/pack/transfer/record")
public class PackTransferRecordController extends BaseController
{
    @Autowired
    private IPackTransferRecordService packTransferRecordService;

    /**
     * 查询移库记录列表
     */
    @PreAuthorize("@ss.hasPermi('pack:transfer:list')")
    @GetMapping("/list")
    public TableDataInfo list(PackTransferRecord packTransferRecord)
    {
        startPage();
        List<PackTransferRecord> list = packTransferRecordService.selectPackTransferRecordList(packTransferRecord);
        return getDataTable(list);
    }

    /**
     * 导出移库记录列表
     */
    @PreAuthorize("@ss.hasPermi('pack:transfer:export')")
    @Log(title = "移库记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, PackTransferRecord packTransferRecord)
    {
        List<PackTransferRecord> list = packTransferRecordService.selectPackTransferRecordList(packTransferRecord);
        ExcelUtil<PackTransferRecord> util = new ExcelUtil<PackTransferRecord>(PackTransferRecord.class);
        util.exportExcel(response, list, "移库记录数据");
    }

    /**
     * 获取移库记录详细信息
     */
    @PreAuthorize("@ss.hasPermi('pack:transfer:query')")
    @GetMapping(value = "/{transferId}")
    public AjaxResult getInfo(@PathVariable("transferId") Long transferId)
    {
        return success(packTransferRecordService.selectPackTransferRecordByTransferId(transferId));
    }

    /**
     * 手动移库
     */
    @PreAuthorize("@ss.hasPermi('pack:transfer:add')")
    @Log(title = "手动移库", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody PackTransferRecord packTransferRecord)
    {
        packTransferRecord.setCreateBy(getUsername());
        packTransferRecord.setAutoGenerated("0"); // 手动创建
        return toAjax(packTransferRecordService.insertPackTransferRecord(packTransferRecord));
    }

    /**
     * 获取移库统计信息
     */
    @PreAuthorize("@ss.hasPermi('pack:transfer:statistics')")
    @GetMapping("/statistics")
    public AjaxResult statistics(PackTransferRecord packTransferRecord)
    {
        Map<String, Object> statisticsData = new HashMap<>();
        
        // 查询所有记录
        List<PackTransferRecord> allRecords = packTransferRecordService.selectPackTransferRecordList(packTransferRecord);
        
        // 统计总数
        long totalTransfers = allRecords.size();
        long fullTransfers = allRecords.stream().filter(r -> "1".equals(r.getTransferType())).count();
        long emptyTransfers = allRecords.stream().filter(r -> "2".equals(r.getTransferType())).count();
        long autoTransfers = allRecords.stream().filter(r -> "1".equals(r.getAutoGenerated())).count();
        long manualTransfers = allRecords.stream().filter(r -> "0".equals(r.getAutoGenerated())).count();
        long outboundTransfers = allRecords.stream().filter(r -> "1".equals(r.getTransferStage())).count();
        long inboundTransfers = allRecords.stream().filter(r -> "2".equals(r.getTransferStage())).count();
        
        statisticsData.put("totalTransfers", totalTransfers);
        statisticsData.put("fullTransfers", fullTransfers);
        statisticsData.put("emptyTransfers", emptyTransfers);
        statisticsData.put("autoTransfers", autoTransfers);
        statisticsData.put("manualTransfers", manualTransfers);
        statisticsData.put("outboundTransfers", outboundTransfers);
        statisticsData.put("inboundTransfers", inboundTransfers);
        
        return success(statisticsData);
    }

    /**
     * 根据器具ID查询移库记录
     */
    @PreAuthorize("@ss.hasPermi('pack:transfer:query')")
    @GetMapping("/container/{containerId}")
    public TableDataInfo getByContainerId(@PathVariable("containerId") Long containerId)
    {
        startPage();
        PackTransferRecord query = new PackTransferRecord();
        query.setContainerId(containerId);
        List<PackTransferRecord> list = packTransferRecordService.selectPackTransferRecordList(query);
        return getDataTable(list);
    }

    /**
     * 根据项目ID查询移库记录
     */
    @PreAuthorize("@ss.hasPermi('pack:transfer:query')")
    @GetMapping("/project/{projectId}")
    public TableDataInfo getByProjectId(@PathVariable("projectId") Long projectId)
    {
        startPage();
        PackTransferRecord query = new PackTransferRecord();
        query.setProjectId(projectId);
        List<PackTransferRecord> list = packTransferRecordService.selectPackTransferRecordList(query);
        return getDataTable(list);
    }

    /**
     * 手动移库请求参数
     */
    public static class ManualTransferRequest {
        private Long containerId;
        private Long projectId;
        private String materialCode;
        private String materialName;
        private String materialQuantity;
        private String liningScheme;
        private Long sourceWarehouseId;
        private Long targetWarehouseId;
        private String transferType;
        private String transferStage;
        private String remark;

        public Long getContainerId() {
            return containerId;
        }

        public void setContainerId(Long containerId) {
            this.containerId = containerId;
        }

        public Long getProjectId() {
            return projectId;
        }

        public void setProjectId(Long projectId) {
            this.projectId = projectId;
        }

        public String getMaterialCode() {
            return materialCode;
        }

        public void setMaterialCode(String materialCode) {
            this.materialCode = materialCode;
        }

        public String getMaterialName() {
            return materialName;
        }

        public void setMaterialName(String materialName) {
            this.materialName = materialName;
        }

        public String getMaterialQuantity() {
            return materialQuantity;
        }

        public void setMaterialQuantity(String materialQuantity) {
            this.materialQuantity = materialQuantity;
        }

        public String getLiningScheme() {
            return liningScheme;
        }

        public void setLiningScheme(String liningScheme) {
            this.liningScheme = liningScheme;
        }

        public Long getSourceWarehouseId() {
            return sourceWarehouseId;
        }

        public void setSourceWarehouseId(Long sourceWarehouseId) {
            this.sourceWarehouseId = sourceWarehouseId;
        }

        public Long getTargetWarehouseId() {
            return targetWarehouseId;
        }

        public void setTargetWarehouseId(Long targetWarehouseId) {
            this.targetWarehouseId = targetWarehouseId;
        }

        public String getTransferType() {
            return transferType;
        }

        public void setTransferType(String transferType) {
            this.transferType = transferType;
        }

        public String getTransferStage() {
            return transferStage;
        }

        public void setTransferStage(String transferStage) {
            this.transferStage = transferStage;
        }

        public String getRemark() {
            return remark;
        }

        public void setRemark(String remark) {
            this.remark = remark;
        }
    }
} 